#ifndef MAININTERFACE_H
#define MAININTERFACE_H

#include <QWidget>

#include <QMessageBox>
#include <QTcpSocket>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QByteArray>
#include "define.h"
#include "networkmanager.h"
#include "studentterminal.h"

class MFHandleServerClass;
using MFHandleServerptr = std::shared_ptr<MFHandleServerClass>;     // 智能指针
MFHandleServerptr GetHandlePtr(const int &type);      // 函数原型（简单工厂）


namespace Ui {
class MainInterface;
}

struct WidgetPrivate;

class MainInterface : public QWidget
{
    Q_OBJECT

public:
    explicit MainInterface(QWidget *parent = nullptr,NetworkManager *networkManager = nullptr);
    ~MainInterface();

signals:
    void ReginBtnClicked(); // 登录按钮点击信号
    void ReginSuccess();    // 登录成功的信号

public slots:
    void onResButtonClicked();  // 登录按钮点击的槽函数

    void readServerResponse();  // 读取服务器响应


signals:
    void RegisterBtnClicked(QString ID,QString passwd);   // 登录按钮点击信号

private:
    Ui::MainInterface *ui;
    QTcpSocket *socket;     // 用于与服务器通信的套接字

    QByteArray buffer;      // 缓冲区

    MFHandleServerptr handle;   // 具体要处理的事情 (见下面)

    NetworkManager *networkManager;

};


class MFHandleServerClass
{
public:
    virtual void MFHandle(QJsonObject responce,MainInterface *maininterface) = 0;
};

#define RegisterClass(Name) \
class Name:public MFHandleServerClass \
{ \
public: \
    void MFHandle(QJsonObject responce,MainInterface *maininterface)override; \
};

RegisterClass(REGISSUCCESS);    // 登录成功
RegisterClass(REGISERROE);      // 登录失败

#endif // MAININTERFACE_H
